home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / ems4c.zip / EMS_TEST.C < prev    next >
Text File  |  1993-06-23  |  5KB  |  153 lines

  1. /*
  2. **                ---  ems_test.c ---
  3. **
  4. **  EXAMPLE CODE: The program tests each of the EMS4C library functions, and
  5. **  exercises the page to frame mapping function by generating random mapping
  6. **  requests.
  7. **
  8. **  This example program (not the EMS4C library) is donated to
  9. **  the Public Domain by MarshallSoft Computing, Inc. It is
  10. **  provided as an example of the use of the EMS4C.
  11. **
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <dos.h>
  16. #include "ems4c.h"
  17.  
  18. #define MAKE_FAR_PTR(seg,off) ((void far *)(((unsigned long)(seg)<<16)|(unsigned)(off)))
  19.  
  20. int RefCount[512];   /* reference count per EMS page */
  21. int Handle = -1;     /* EMS handle */
  22.  
  23. void main()
  24. {int i, k;        /* loop counters */
  25.  int Count;       /* reference count in page */
  26.  int Lap;         /* loop counter */
  27.  int Page;        /* EMS logical page */
  28.  int ErrorNumber; /* error number return from EMS4C */
  29.  int Version;     /* EMS version number */
  30.  int FreePages;   /* number of free EMS pages */
  31.  int TotalPages;  /* total number of EMS pages */
  32.  int Frame;       /* EMS frame number [0,1,2,3] */
  33.  int FrameSeg;    /* Segment associated with <Frame> */
  34.  int Active = 0;  /* number of active EMS handles */
  35.  int AllocPages;  /* number of EMS pages to allocate */
  36.  int Total = 0;   /* total accumulated reference count */
  37.  int OwnedPages;  /* number of pages owned by this handle */
  38.  char SrcArray[1];/* dummy Page Map source array */
  39.  char DstArray[1];/* dummy Page Map destination array */
  40.  int Result = -1; /* result variable */
  41.  char far *FarCharPtr;  /* generic far character pointer */
  42.  int far *FarIntPtr;    /* generic far integer pointer */
  43.  /* is EMS driver loaded ? */
  44.  ErrorCheck( emsLoaded() );
  45.  printf("EMS driver is loaded\n");
  46.  /* get EMM manager status */
  47.  ErrorCheck( emsGetStatus() );
  48.  printf("EMS status is OK\n");
  49.  /* get EMM version */
  50.  ErrorCheck( emsVersion(&Version) );
  51.  printf("EMM Version = %x.%x\n",Version/16,0x0f&Version);
  52.  /* get total number of EMS pages */
  53.  ErrorCheck( emsGetPages(&FreePages,&TotalPages) );
  54.  printf("Free EMS Pages = %d (%xH)\n",FreePages,FreePages);
  55.  printf("Total EMS Pages = %d (%dH)\n",TotalPages,TotalPages);
  56.  /* get # free EMS handles */
  57.  ErrorCheck( emsGetHandles(&Active) );
  58.  printf("Free handles = %d\n",255-Active);
  59.  /* get EMS frame address */
  60.  ErrorCheck( emsGetFrame(&FrameSeg) );
  61.  printf("Frame Address = %x:0\n",FrameSeg);
  62.  puts("Beginning EMS testing...");
  63.  /* clear each 16K frame */
  64.  for(i=0;i<4;i++)
  65.    {/* make pointer to start of 16K frame */
  66.     FarCharPtr = MAKE_FAR_PTR(FrameSeg+i*1024,0);
  67.     /* clear 16K bytes */
  68.     printf("Clearing frame %d at %x:0\n",i,FrameSeg+i*1024);
  69.     for(k=0;k<16384;k++) *FarCharPtr++ = '\0';
  70.    }
  71.  /* allocate up to 512 pages */
  72.  if(FreePages<512) AllocPages = FreePages;
  73.  else AllocPages = 512;
  74.  for(i=0;i<AllocPages;i++) RefCount[i] = 0;
  75.  printf("Allocating %d pages\n",AllocPages);
  76.  ErrorCheck( emsAllocate(AllocPages,&Handle) );
  77.  printf("EMS Handle = %d\n",Handle);
  78.  /* expect # owned pages = allocated pages */
  79.  ErrorCheck( emsOwnedPages(Handle,&OwnedPages) );
  80.  if(OwnedPages!=AllocPages)
  81.    printf("WARNING: OwnedPages=%d != AllocPages=%d\n",OwnedPages,AllocPages);
  82.  /* Free pages should now be zero */
  83.  ErrorCheck( emsGetPages(&FreePages,&TotalPages) );
  84.  if(FreePages!=0) printf("WARNING: Free page count %d not 0\n",FreePages);
  85.  puts("Begin random access test...");
  86.  for(Lap=0;Lap<(5*AllocPages);Lap++)
  87.    {/* select frame from [0..3] */
  88.     Frame = Random(4);
  89.     FarIntPtr = MAKE_FAR_PTR(FrameSeg+1024*Frame,0);
  90.     /* select page from [0..AllocPages-1] */
  91.     Page = Random(AllocPages);
  92. #if 0
  93. printf("%d: Mapping page %d to frame %d\n",Lap,Page,Frame);
  94. #endif
  95.     /* map page */
  96.     ErrorCheck( emsMapMemory(Frame,Page,Handle) );
  97.     if(RefCount[Page]==0)
  98.       {/* write 0 to page */
  99.        Count = 0;
  100.       }
  101.     else
  102.       {/* */
  103.        Count = *FarIntPtr;
  104.        if(Count!=RefCount[Page])
  105.          {printf("ERROR: Found ref count %d, expected %d for page %d\n",
  106.            Count,RefCount[Page],Page);
  107.           break;
  108.          }
  109.       }
  110.     /* increment ref count */
  111.     Count++;
  112.     RefCount[Page] = Count;
  113.     *FarIntPtr = Count;
  114. #if 0
  115. printf("%d: RefCount[%d]=%d\n",Lap,Page,Count);
  116. #endif
  117.    } /* end for */
  118. #if 0
  119.  /* display RefCount[] array */
  120.  for(i=0;i<AllocPages;i++)
  121.      printf("RefCount[%d] = %d\n",i,RefCount[i]);
  122. #endif
  123.  for(i=0;i<AllocPages;i++) Total += RefCount[i];
  124.  printf("Average reference count = %f\n",
  125.    (float)Total / (float)AllocPages);
  126.  /* save & restore mapping context */
  127.  ErrorCheck( emsSaveMap(Handle) );
  128.  ErrorCheck( emsRestoreMap(Handle) );
  129.  /* free allocated pages */
  130.  ErrorCheck( emsRelease(Handle) );
  131.  /* display page map array size */
  132.  ErrorCheck( emsPageMap(3,SrcArray,DstArray,&Result) );
  133.  printf("EMS Page Map size = %d\n",Result);
  134.  puts("Test completed\n");
  135. } /* end main */
  136.  
  137. int ErrorCheck(ErrorNumber)
  138. int ErrorNumber;
  139. {if(ErrorNumber)
  140.    {emsError(ErrorNumber);
  141.     if(Handle>=0) emsRelease(Handle);
  142.     exit(1);
  143.    }
  144.  return(ErrorNumber);
  145. } /* end ErrorCheck */
  146.  
  147. int Random(Modulo)
  148. int Modulo;
  149. {int r;
  150.  r = rand();
  151.  return(r%Modulo);
  152. }
  153.